package com.yunchen.BooksManagementSystem.user.dao;

import com.yunchen.BooksManagementSystem.login.entity.User;
import com.yunchen.BooksManagementSystem.manage.entity.borrowingRecord;
import com.yunchen.BooksManagementSystem.user.entity.Books;
import com.yunchen.BooksManagementSystem.user.entity.UserInformation;
import com.yunchen.BooksManagementSystem.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;

public class  userDaoImpl implements userDao{
    private UserInformation user;
    private Books book;

    public userDaoImpl() {
    }

    public userDaoImpl(UserInformation user, Books book) {
        this.user = user;
        this.book = book;
    }

    public ArrayList<Books> getAllBooks() throws Exception{
        Connection conn = DBUtil.getConnection();
        Statement stmt = conn.createStatement();
        String sql = "select * from booksinfo";
        ResultSet rs = stmt.executeQuery(sql);
        ArrayList<Books> books = new ArrayList<>();
        while (rs.next()) {
            Books book=new Books();
            book.setPicture(rs.getString("picture"));
            book.setBookName(rs.getNString("bookName"));
            book.setAuthor(rs.getString("author"));
            book.setCategory(rs.getString("category"));
            book.setAmount(rs.getInt("number"));
            book.setIntroduction(rs.getString("introduction"));
            books.add(book);
        }
        System.out.println(books);
        rs.close();
        stmt.close();
        return books;
    }

    public ArrayList<Books> getBooksByCategory(String category) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql = "select * from booksinfo where category = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,category);
        ResultSet rs = pstmt.executeQuery();
        ArrayList<Books> books = new ArrayList<>();
        while (rs.next()) {
            Books book=new Books();
         //   book.setPicture(rs.getString("picture"));
            book.setBookName(rs.getNString("bookName"));
            book.setAuthor(rs.getString("author"));
            book.setCategory(rs.getString("category"));
            book.setAmount(rs.getInt("number"));
            book.setIntroduction(rs.getString("introduction"));
            books.add(book);
            System.out.println(book);
        }

        System.out.println(books);
        System.out.println(111);
        rs.close();
        pstmt.close();
        return books;
    }

    public ArrayList<UserInformation> personalInformation(String userName) throws Exception {
        Connection conn = DBUtil.getConnection();
        String sql = "select * from userinfo where name = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,userName);
        ResultSet rs = pstmt.executeQuery();
        ArrayList<UserInformation> userInfo = new ArrayList<>();
        while (rs.next()) {
            UserInformation userInformation=new UserInformation();
            userInformation.setUserId(rs.getString("userId"));
            userInformation.setUserName(rs.getString("name"));
            userInformation.setGrade(rs.getString("grade"));
            userInformation.setDirection(rs.getString("direction"));
            userInfo.add(userInformation);
        }
        rs.close();
        pstmt.close();
        return userInfo;
    }

    public int borrowBook(String userName,String bookName) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql = "insert into borrowingrecord(name,amount,bookName,status) values(?,1,?,'申请借阅')";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,userName);
        pstmt.setString(2,bookName);
        int count = pstmt.executeUpdate();
        if (count>0){
            String sql2 = "update booksinfo set number=number-1 where bookName=bookName";
            Statement stmt = conn.createStatement();
            count = stmt.executeUpdate(sql2);
            stmt.close();
        }
        pstmt.close();
        return count;
    }

    public int returnBook(String userName,String bookName) throws Exception{
        Connection conn = DBUtil.getConnection();
        String sql ="update borrowingrecord set status='申请还书' where name=? and bookName=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,userName);
        pstmt.setString(2,bookName);
        int count = pstmt.executeUpdate();
        if (count>0){
            String sql2 ="update booksinfo set number=number+1 where bookName=bookName";
            Statement stmt = conn.createStatement();
            count = stmt.executeUpdate(sql2);
            stmt.close();
        }
        pstmt.close();
        return count;
    }

    //查看正在借阅
    public ArrayList<borrowingRecord> userBorrowing(String userName) throws Exception{
        ArrayList<borrowingRecord> borrowingRecords = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        String sql = "select * from borrowingrecord where name=? and status='借书中'";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,userName);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()){
            borrowingRecord user = new borrowingRecord();
            user.setName(rs.getString("name"));
            user.setNumber(rs.getInt("amount"));
            user.setBookName(rs.getString("bookName"));
            user.setBorrowTime(rs.getDate("borrowTime"));
            user.setReturnTime(rs.getDate("returnTime"));
            user.setStatus(rs.getString("status"));
            borrowingRecords.add(user);
        }
        System.out.println(borrowingRecords);
        rs.close();
        pstmt.close();
        return borrowingRecords;
    }

    //用户借书记录
    public ArrayList<borrowingRecord> history(String userName) throws Exception{
        ArrayList<borrowingRecord> borrowingRecords = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        String sql = "select * from borrowingrecord where name=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,userName);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()){
            borrowingRecord user = new borrowingRecord();
            user.setName(rs.getString("name"));
            user.setNumber(rs.getInt("amount"));
            user.setBookName(rs.getString("bookName"));
            user.setBorrowTime(rs.getDate("borrowTime"));
            user.setReturnTime(rs.getDate("returnTime"));
            user.setStatus(rs.getString("status"));
            borrowingRecords.add(user);
        }
        System.out.println(borrowingRecords);
        rs.close();
        pstmt.close();
        return borrowingRecords;
    }

}
